﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Models;
using JinfuyuanProject.Repository.Interface;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Repository.Repository
{
    public class MParturientRepository : IMParturientRepository
    {
        private readonly JinfuyuanMongoDbContext _db;
        public MParturientRepository(JinfuyuanMongoDbContext db)
        {
            _db = db;
        }
        public int Delete(MParturient Parturient)
        {
           var Bson = new BsonDocument
            {
                { "ParturientName", Parturient.ParturientName }
            };
            var result = _db.Parturients.DeleteOne(Bson);
            return (int)result.DeletedCount;
        }

        public List<MParturient> GetList()
        {
          return _db.Parturients.Find(new BsonDocument()).ToList();
        }

        public MParturient GetModel(string name)
        {
            var filter = Builders<MParturient>.Filter.Eq("ParturientName", name);
            var result = _db.Parturients.Find(filter).FirstOrDefault();
            return result;
        }

        public int Insert(MParturient Parturient)
        {
            int result = 0;
            try {
                _db.Parturients.InsertOne(Parturient);
                result = 1;
            }
            catch (Exception ex)
            {
                throw new Exception("插入数据失败", ex);  
            }
            return result;
        }

        public int  Update(MParturient Parturient)
        {
            
            int result = 0;
            try
            {
                var filter = Builders<MParturient>.Filter.Eq("ParturientName", Parturient.ParturientName);

                var updateDefinitions = new List<UpdateDefinition<MParturient>>();

                // 根据条件添加更新操作
                if (Parturient.ParturientUpdateName != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("ParturientName", Parturient.ParturientUpdateName));
                if (Parturient.ParturientAge != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("ParturientAge", Parturient.ParturientAge));
                if (Parturient.ParturientPhone != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("ParturientPhone", Parturient.ParturientPhone));
                if (Parturient.Address != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("Address", Parturient.Address));
                if (Parturient.Salesman != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("Salesman", Parturient.Salesman));
                if (Parturient.SalesmanNumbers != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("SalesmanNumbers", Parturient.SalesmanNumbers));
                if(Parturient.EmergencyContacts!=null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("EmergencyContacts", Parturient.EmergencyContacts));
                if (Parturient.EmergencyContactsPhone != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("EmergencyContactsPhone", Parturient.EmergencyContactsPhone));
                if (Parturient.BirthTime != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("BirthTime", Parturient.BirthTime));
                if (Parturient.EntryTime != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("EntryTime", Parturient.EntryTime));
                if (Parturient.Degree != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("Degree", Parturient.Degree));
                if (Parturient.PerinatalHospitals != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("PerinatalHospitals", Parturient.PerinatalHospitals));
                if (Parturient.WeightDetail != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("WeightDetail", Parturient.WeightDetail));
                if(Parturient.Weight != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("Weight", Parturient.Weight));
                if (Parturient.Revisits!=null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("Revisits", Parturient.Revisits));
                if (Parturient.IsDelete != null)
                    updateDefinitions.Add(Builders<MParturient>.Update.Set("IsDelete", Parturient.IsDelete));
                // 如果有更新操作，组合所有更新
                if (updateDefinitions.Count > 0)
                {
                    var update = Builders<MParturient>.Update.Combine(updateDefinitions);

                    // 执行更新操作
                    var result1 = _db.Parturients.UpdateOne(filter, update);

                    result = (int)result1.ModifiedCount;
                }
                else
                {
                    result = 0; // 没有更新操作   
                }
                }
            catch (Exception ex)
            {
                throw new Exception("更新数据失败", ex);
            }
            return result;
        }
    }
}
